﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using SAP.Middleware.Connector;
using Casco.Erz.Models;

namespace Casco.Erz.DAL
{
    public class Position_sapContext
    {
        IDestinationConfiguration ID;
        RfcDestination Prd;
        List<Position> _Positions;
        public List<Position> Positions { get { 
            /*获取职位*/
            RfcRepository repo = Prd.Repository;
            IRfcFunction PostingBapi = repo.CreateFunction("ZERC_EXTERNAL_POSTING");   //调用函数名
            /*参数*/
            
            PostingBapi.SetValue("BEGDA", "20140203");   //设置Import的参数
            PostingBapi.SetValue("ENDDA", "99991231");   //设置Import的参数

            PostingBapi.Invoke(Prd);   //执行函数
            IRfcTable PostTab = PostingBapi.GetTable("POSTING");  //获取表内容
            if (PostTab != null)
            {
                for (int i = 0; i < PostTab.Count; i++)
                {
                    PostTab.CurrentIndex = i;
                    Position PosItem = new Position()
                    {
                        PositionID = Convert.ToInt32(PostTab.GetString("OBJID")),
                        Header = PostTab.GetString("HEADER"),
                        BegDate = PostTab.GetString("ZSTART_DATE"),
                        EndDate = PostTab.GetString("ZEND_DATE"),

                        Gljy = PostTab.GetString("ZHRGLJYTXT"),
                        GzExper = PostTab.GetString("ZHRGZJYTXT"),
                        GzPlace = PostTab.GetString("ZHRGZTXT"),
                        Zdxl = PostTab.GetString("ZHRZDXLTXT"),

                        ZpDept = PostTab.GetString("ZDES1"),
                        ZpDuety = PostTab.GetString("ZDES2"),
                        ZpPlans = PostTab.GetString("ZDES3"),
                        ZpRequire = PostTab.GetString("ZDES4"),

                        HtType = PostTab.GetString("DESCRIPTION"),
                        ZwType = PostTab.GetString("ZHRZWLBTXT")
                    };
                    _Positions.Add(PosItem);
                }
                return _Positions;
            }
            else { return null; }
        } }
        public Position_sapContext() {
            /*字段初始化*/
            _Positions = new List<Position>();
            /*配置Sap*/
            ID = new MyBackendConfig();

            RfcDestinationManager.RegisterDestinationConfiguration(ID);

            Prd = RfcDestinationManager.GetDestination("DES_000");

            RfcDestinationManager.UnregisterDestinationConfiguration(ID);
        }
    }
    public class MyBackendConfig : IDestinationConfiguration
    {
        public RfcConfigParameters GetParameters(String destinationName)
        {

            if ("DES_000".Equals(destinationName))
            {
                RfcConfigParameters parms = new RfcConfigParameters();
                parms.Add(RfcConfigParameters.AppServerHost, "192.100.96.19");   //SAP主机IP
                parms.Add(RfcConfigParameters.SystemNumber, "00");  //SAP实例
                parms.Add(RfcConfigParameters.User, "ADMIN");  //用户名
                parms.Add(RfcConfigParameters.Password, "Cascohcm789");  //密码
                parms.Add(RfcConfigParameters.Client, "970");  // Client
                parms.Add(RfcConfigParameters.Language, "ZH");  //登陆语言
                parms.Add(RfcConfigParameters.PoolSize, "5");
                parms.Add(RfcConfigParameters.MaxPoolSize, "10");
                parms.Add(RfcConfigParameters.IdleTimeout, "60");
                return parms;
            }
            else return null;
        }
        public bool ChangeEventsSupported()
        {
            return false;
        }
        public event RfcDestinationManager.ConfigurationChangeHandler ConfigurationChanged;
    }
}